//
//  UIViewController+XWXPresentMethod.m
//  sishunwlcar
//
//  Created by Mr_hi on 2020/3/2.
//  Copyright © 2020 zwhl. All rights reserved.
//

#import "UIViewController+XWXPresentMethod.h"
#import <objc/runtime.h>


@implementation UIViewController (XWXPresentMethod)

/**
   对于13及以上的系统 presentViewController的问题
   会出现这种情况是主要是因为我们之前对UIViewController里面的一个属性，即modalPresentationStyle（该属性是控制器在模态视图时将要使用的样式）没有设置需要的类型。在iOS13中modalPresentationStyle的默认改为UIModalPresentationAutomatic,而在之前默认是UIModalPresentationFullScreen。
   这个不受Xcode版本的影响，只要是iOS13的系统，没有显式设置modalPresentationStyle，就会有这个问题
   VC.modalPresentationStyle = UIModalPresentationFullScreen;
*/

+ (void)load{
    [super load];
    
    SEL originalSel = @selector(presentViewController:animated:completion:);
    SEL overrideSel = @selector(override_presentViewController:animated:completion:);
    
    Method originalMet = class_getInstanceMethod(self.class, originalSel);
    Method overrideMet = class_getInstanceMethod(self.class, overrideSel);
    
    method_exchangeImplementations(originalMet, overrideMet);
}

// Swizzling
- (void)override_presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion{
    if(@available(iOS 13.0, *)){
        if (viewControllerToPresent.modalPresentationStyle ==  UIModalPresentationPageSheet){
            viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
        }
    }
    
    [self override_presentViewController:viewControllerToPresent animated:flag completion:completion];
}

@end
